package com.css.prompt.service;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.css.prompt.model.MyFolder;
import com.css.prompt.repository.MyFolderRepository;

@Service
public class MyFolderService {

	private final MyFolderRepository myFolderRepository;

    public MyFolderService(MyFolderRepository myFolderRepository) {
        this.myFolderRepository = myFolderRepository;
    }

    public List<MyFolder> getAllMyFolders() {
        return myFolderRepository.findAll();
    }

    public Optional<MyFolder> getMyFolderById(Long id) {
        return myFolderRepository.findById(id);
    }

    public MyFolder createMyFolder(MyFolder model) {
    	model.setUpdatedAt(LocalDate.now());
    	model.setCreatedAt(LocalDate.now());
        return myFolderRepository.save(model);
    }

    public MyFolder updateMyFolder(Long id, MyFolder updatedMyFolder) {
        return myFolderRepository.findById(id)
                .map(existingModel -> {
                    existingModel.setName(updatedMyFolder.getName());
                    existingModel.setUpdatedAt(LocalDate.now());
                    return myFolderRepository.save(existingModel);
                })
                .orElseThrow(() -> new RuntimeException("Model not found"));
    }

    public void deleteMyFolder(Long id) {
        myFolderRepository.deleteById(id);
    }
    
    public Page<MyFolder> getPage(Pageable pageable) {
        return myFolderRepository.findAll(pageable);
    }

}
